using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PengYouClient
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();
        }

        private void SetText()
        {
            tabPage1.Text = LanguageManager.GetString("WindowSettingsAccount");
            tabPage2.Text = LanguageManager.GetString("WindowSettingsAdvanced");
            tabPage3.Text = LanguageManager.GetString("WindowSettingsLanguage");
            UserInfoGrpBox.Text = LanguageManager.GetString("WindowSettingsUserInfo");
            ServerAccessGrpBox.Text = LanguageManager.GetString("WindowSettingsServerAccess");
            Username_label.Text = LanguageManager.GetString("WindowSettingsUsername");
            Password_label.Text = LanguageManager.GetString("WindowSettingsPassword");
            Server_address_label.Text = LanguageManager.GetString("WindowSettingsServerAdress");
            Server_port_label.Text = LanguageManager.GetString("WindowSettingsServerPort");
            Ressource_label.Text = LanguageManager.GetString("WindowSettingsDefaultRess");
            UseProxyGrpBox.Text = LanguageManager.GetString("WindowSettingsConnectThroughProxy");
            UseProxy_CheckBox.Text = LanguageManager.GetString("WindowSettingsUseProxy");
            ProxyGrpBox.Text = LanguageManager.GetString("WindowSettingsProxyServer");
            ProxyHost_label.Text = LanguageManager.GetString("WindowSettingsProxyAddress");
            ProxyPass_Label.Text = LanguageManager.GetString("WindowSettingsProxyPassword");
            ProxyPort_label.Text = LanguageManager.GetString("WindowSettingsProxyPort");
            ProxyUser_label.Text = LanguageManager.GetString("WindowSettingsProxyUserName");
            Language_box.Text = LanguageManager.GetString("WindowSettingsProxyLangSelection");
            WordRestart_label.Text = LanguageManager.GetString("WindowSettingsRestartWord");
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            SetText();
            // Account part
            string language;

            Username_box.Text = SettingsManager.Server_user;
            Password_box.Text = SettingsManager.Server_pass;
            Server_address_box.Text = SettingsManager.Server_host;
            Server_port_box.Text = SettingsManager.Server_port;
            Ressource_box.Text = SettingsManager.Ressource_basedir;
            language = SettingsManager.language;

            switch (language)
            {
                case "fr-FR":
                    {
                        Language_box.Text = "French";
                        break;
                    }
                case "en-EN":
                    {
                        Language_box.Text = "English";
                        break;
                    }
            }
            // Advanced part

            if (SettingsManager.Proxy_useproxy == "true")
                UseProxy_CheckBox.Checked = true;
            else
            {
                UseProxy_CheckBox.Checked = false;
                ProxyHost_box.Enabled = false;
                ProxyPass_box.Enabled = false;
                ProxyPort_box.Enabled = false;
                ProxyUser_box.Enabled = false;
            }

            ProxyHost_box.Text = SettingsManager.Proxy_host;
            ProxyPass_box.Text = SettingsManager.Proxy_pass;
            ProxyPort_box.Text = SettingsManager.Proxy_port;
            ProxyUser_box.Text = SettingsManager.Proxy_user;
        }

        private void Cancel_button_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SetLanguage()
        {
            switch (Language_box.Text)
            {
                case "French":
                    {
                        SettingsManager.UpdateVar("Locales", "language", "fr-FR");
                        break;
                    }
                case "English":
                    {
                        SettingsManager.UpdateVar("Locales", "language", "en-EN");
                        break;
                    }
            }
        }

        private void Ok_button_Click(object sender, EventArgs e)
        {
            if (Username_box.Modified)
                SettingsManager.UpdateVar("Server", "user", Username_box.Text);
            if (Password_box.Modified)
                SettingsManager.UpdateVar("Server", "pass", Password_box.Text);
            if (Server_address_box.Modified)
                SettingsManager.UpdateVar("Server", "host", Server_address_box.Text);
            if (Server_port_box.Modified)
                SettingsManager.UpdateVar("Server", "port", Server_port_box.Text);
            if (Ressource_box.Modified)
                SettingsManager.UpdateVar("Ressource", "basedir", Ressource_box.Text);
            if (UseProxy_CheckBox.Checked)
                SettingsManager.UpdateVar("Proxy", "useproxy", "true");
            else
                SettingsManager.UpdateVar("Proxy", "useproxy", "false");
            if (ProxyHost_box.Modified)
                SettingsManager.UpdateVar("Proxy", "host", ProxyHost_box.Text);
            if (ProxyPort_box.Modified)
                SettingsManager.UpdateVar("Proxy", "port", ProxyPort_box.Text);
            if (ProxyUser_box.Modified)
                SettingsManager.UpdateVar("Proxy", "user", ProxyUser_box.Text);
            if (ProxyPass_box.Modified)
                SettingsManager.UpdateVar("Proxy", "pass", ProxyPass_box.Text);
            SetLanguage();
            SettingsManager.readXML();
            this.Close();
        }

        private void UseProxy_CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (UseProxy_CheckBox.Checked)
            {
                ProxyHost_box.Enabled = true;
                ProxyPass_box.Enabled = true;
                ProxyPort_box.Enabled = true;
                ProxyUser_box.Enabled = true;
            }
            else
            {
                ProxyHost_box.Enabled = false;
                ProxyPass_box.Enabled = false;
                ProxyPort_box.Enabled = false;
                ProxyUser_box.Enabled = false;
            }
        }

    }
}